package {
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	
	import demo.isolib.Avatar;
	import demo.isolib.Directions;
	import demo.isolib.SkinDescriptor;
	import demo.isolib.SpriteSheet;
	import demo.isolib.Tile;
	import demo.isolib.World;
	import demo.isolib.assets.Assets;
	import demo.isolib.mousehandler.DummyMouseHandler;
	import demo.isolib.movement.AIMove;
	import demo.isolib.movement.BasicMove;
	import demo.isolib.movement.SmoothMove;
	import demo.isolib.movement.Wander;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	IsoView.zoomContainer.mContainer.sceneContainer


	IsoView.maskShape

	IsoView.borderShape		// the border of IsoView

	IsoContainer.mainContainer
	IsoScene extends IsoContainer
  
	 */
	public class Extended_v0_final extends Sprite {
		private var _world:World;

		public function Extended_v0_final() {
			trace("Extended_v0_final ?");
			super.addEventListener(Event.ADDED_TO_STAGE, this.init);
		}

		private function init(e:Event):void {
			super.removeEventListener(Event.ADDED_TO_STAGE, this.init);

			this.createWorld();
			this.createObjects();	
			
			return;
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE, this.onXMLLoaded);
			urlloader.load(new URLRequest("./resources/objectlib.xml"));
		}

		private function onXMLLoaded(evt:Event):void {
			
			var xml:XML = new XML((evt.target) as URLLoader).data;
			
			trace(xml);
			
			return ;
			
			this.createWorld();
			this.createObjects();			
		}
		
		private function createWorld():void {
			//1. buat world instance
			var gridCols:Number = 21; 
			var gridRows:Number = 21;
			var gridCellsize:Number = 20;
			var W:Number = 21*20*2;
			var H:Number = 21*20*2;
			
			this._world = new World(W, H, gridCols, gridRows, gridCellsize);
			//_world.setBackground(new Assets.WORLD_BACKGROUND(), -400, -50);
			this._world.setBackground(this.createBGShape(gridCols*gridCellsize*2, gridRows*gridCellsize*2), -1*gridCols*gridCellsize, -1*gridRows*gridCellsize/2);
			//this._world.setBackground(new Assets.WORLD_BACKGROUND(), -1*gridCols*gridCellsize, -1*gridRows*gridCellsize/2);
			
			//2. set debug mode
			this._world.debugMode = true;
			//3. panning
			this._world.panTo(0, 210);
			//this._world.currentZoom = 0.6;
			this._world.x = -1 * W/4;
			this._world.y = -1 * H/4;
			//4. tambahkan world ke display list
			addChild(_world);
			trace("WXY="+this._world.x+", "+this._world.y+", "+this._world.width+", "+this._world.height);
			
		}

		private function createObjects():void {
			// avatar
			var avatar:Tile = createAnimatedAvatar(1, 1, 80, Assets.MALE_0, Assets.CHAR_DESCRIPTOR);
			avatar.name = "avatar";
			avatar.faceDirection = Directions.N;
			avatar.moveBehavior = new SmoothMove(100);
			avatar.selected = false;
			this._world.addAvatar(avatar, 10, 10);
	
			
			/*
	<item offsetX="14" offsetY="-7" regX="130" regY="211" 
	
label="resources/png/item/special/mcdonnell's_barn.png" url="media/png/item/special/mcdonnell's_barn.png" 
block="106,175;130,187;154,199;178,211;" +
	"154,223;130,235;106,247;82,235;
			58,223;34,211;82,211;58,199;
			106,223;"130,211;106,199;82,187;"/>

		<item offsetX="7" offsetY="-4" regX="65" regY="112" 

label="resources/png/item/special/zombie camera.png" url="media/png/item/special/zombie camera.png" block="41,112;"/>
*/
				
			/*
			var g0:Tile = createAnimatedCharacter(1, 1, 80, Assets.FEMALE_1, Assets.CHAR_DESCRIPTOR)
			g0.name = "Girl0";
			g0.selectable = false;
			g0.moveBehavior = new Wander(5000);
			g0.moveBehavior.execute();
			_world.addTile(g0, 15, 9);
			*/
			
			var skind:DisplayObject;

			skind = new Assets.A();
			var a:Tile = new Tile(4, 4, 0);
			a.addSkin(skind, new SkinDescriptor(14, -7));
			_world.addTile(a, 10, 5);
			
			/*
			//var skin:DisplayObject = new Assets.FLOOR();
			var skin:DisplayObject = new Assets.TREE_0();
		//	var isoPt:Pt = IsoMath.screenToIso(new Pt(skin.width, skin.height), true);
			var isoPt:Pt = new Pt(0, 0, 0);
			var floor:Tile = new Tile(2, 2, 0);
			floor.name = "floor";
			floor.dragable = false;
			floor.addSkin(skin, new SkinDescriptor(-40, -110, 0.5, 0.5));
			_world.addTile(floor, 10, 5);
			*/
			
			
			var bct:Tile = new Tile(2,2, 0);
			bct.name = "box";
			bct.addSkin(new Assets.CRATE(), new SkinDescriptor(0, 0, 1, 1));			
			_world.addTile(bct, 4, 10);
			
			return;
			
			
			_world.addTile(new Tile(3, 2), 13, 8);
			//non-selectable tile
			//1. buat tile
			var t0:Tile = new Tile(2, 3);
			//2. set selectable = false
			t0.selectable = false;
			//3. tambahkan ke world
			_world.addTile(t0, 10, 4);

			//////////////////////////////////////////////
			// MOVABLE OBJECTS/TILES (AI)
			//////////////////////////////////////////////
			//1. buat tile baru
			var ait:Tile = new Tile();
			//2. tambahkan tile ke world
			_world.addTile(ait, 0, 5);
			//3. set movebehavior = AIMove
			ait.moveBehavior = new AIMove();

			//////////////////////////////////////////////
			// MOVING OBJECTS/TILES
			//////////////////////////////////////////////
			//1. buat tile baru
			var wt:Tile = new Tile();
			//2. tambahkan tile ke world
			_world.addTile(wt, 0, 0);
			//3. set movebehavior
			wt.moveBehavior = new Wander(1000);
			//4. execute/start movebehavior
			wt.moveBehavior.execute();
		}
		
		private function createBGShape(w:int, h:int):Shape {
			var shape:Shape = new Shape();
			var g:Graphics = shape.graphics;
			g.clear();
			g.beginFill(0x6DA564);
			g.lineStyle(3, 0xFF0000);
			g.drawRect(0, 0, w, h);
			g.endFill();
			return shape;
		}
		
		private function createAnimatedAvatar(cspan:Number, rspan:Number, height:Number, spriteSheetClass:Class, sheetDescriptor:Object):Avatar {
			var t:Avatar = new Avatar(cspan, rspan, height);
			return this.createAnimatedTile(t, spriteSheetClass, sheetDescriptor) as Avatar;
		}
		private function createAnimatedCharacter(cspan:Number, rspan:Number, height:Number, spriteSheetClass:Class, sheetDescriptor:Object):Tile {
			var t:Tile = new Tile(cspan, rspan, height);
			return this.createAnimatedTile(t, spriteSheetClass, sheetDescriptor) as Tile;
		}
		private function createAnimatedTile(t:Tile, spriteSheetClass:Class, sheetDescriptor:Object):Tile {
			var s:SpriteSheet = new SpriteSheet();
			s.build(new spriteSheetClass(), sheetDescriptor.frameWidth, sheetDescriptor.frameHeight);
			t.addAnimationSheet(s, new SkinDescriptor(sheetDescriptor.x, sheetDescriptor.y, sheetDescriptor.scaleX, sheetDescriptor.scaleY));
			return t;
		}
	}
}